package com.base.cn.platform.os.controller.classes.topic.report;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.classes.topic.report.ClassesTopicReport;
import com.base.cn.platform.os.service.classes.topic.report.ClassesTopicReportBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 班级话题举报
 *
 * @author SK
 * @since 2018-06-04
 */
@RestController
@RequestMapping("/course")
public class ClassesTopicReportController extends BaseController {

    @Autowired
    private ClassesTopicReportBiz classesTopicReportBiz;

    /**
     * 查询班级话题举报列表
     *
     * @param classesTopicReport 班级话题举报查询条件
     * @param currentPage        第N页
     * @return 班级话题举报列表
     */
    @RequestMapping("/findClassesTopicReportPage")
    public PageInfo<ClassesTopicReport> findClassesTopicReportPage(@RequestBody ClassesTopicReport classesTopicReport,
                                                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        return classesTopicReportBiz.findClassesTopicReportPage(classesTopicReport, pagination);
    }

    /**
     * 保存班级话题举报
     *
     * @param classesTopicReport 班级话题举报
     * @return 保存结果
     */
    @RequestMapping("/saveClassesTopicReport")
    public Map<String, Object> saveClassesTopicReport(@RequestBody ClassesTopicReport classesTopicReport) {
        Map<String, Object> check = this.checkClassesTopicReport(classesTopicReport);
        if (Objects.nonNull(check)) {
            return check;
        }
        classesTopicReportBiz.save(classesTopicReport);
        return ResultUtil.SUCCESS("举报成功");
    }

    /**
     * 更新话题举报状态
     *
     * @param ids    话题举报id
     * @param status 1.正常 2.删除
     * @return 操作结果
     */
    @RequestMapping("/updateClassesTopicReportStatus")
    public Map<String, Object> updateClassesTopicReportStatus(@RequestParam("ids") String ids,
                                                              @RequestParam("status") Integer status) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("选择操作的话题举报");
        }
        classesTopicReportBiz.updateClassesTopicReportStatus(ids, status);
        return ResultUtil.SUCCESS("修改话题举报状态成功");
    }

    /**
     * 检查班级话题数据
     */
    private Map<String, Object> checkClassesTopicReport(ClassesTopicReport classesTopicReport) {
        if (!DataUtil.idIsNotNull(classesTopicReport.getTopicId())) {
            return ResultUtil.ERROR("举报的话题不存在");
        }
        if (!DataUtil.isPositive(classesTopicReport.getType())) {
            return ResultUtil.ERROR("请选择举报类型");
        }
        if (StringUtils.isEmpty(classesTopicReport.getContent())) {
            return ResultUtil.ERROR("请填写举报内容");
        }
        List<ClassesTopicReport> classesTopicReportList = classesTopicReportBiz.findClassesTopicReportList(classesTopicReport, 1);
        if (ObjectUtils.isNotEmpty(classesTopicReportList)) {
            return ResultUtil.ERROR("你已经举报过了");
        }
        return null;
    }
}